package com.wz.app.adapter;

import java.util.List;

import com.wz.app.R;
import com.wz.app.http.LoadImage;
import com.wz.app.model.Contentlist;
import com.wz.app.model.Imageurls;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NewsAdapter extends BaseAdapter  {

	private List<Contentlist> listData;
	private Context context;
	private LayoutInflater mInflater;
	private LruCache<String, Bitmap> cache;

	public NewsAdapter(List<Contentlist> listData, Context context)  {
		super();
		this.listData = listData;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		// 初始化 缓存
		// 初始化lruCache
		int maxMemory = (int) Runtime.getRuntime().maxMemory();
		int cacheMemory = maxMemory / 4;
		cache = new LruCache<String, Bitmap>(cacheMemory) {
			@Override
			protected int sizeOf(String key, Bitmap value) {
				// TODO Auto-generated method stub
				return value.getByteCount();
			}
		};

	}

	public Bitmap getBitMapFromCache(String url) {
		return cache.get(url);
	}

	public void setBitMapToCache(String url, Bitmap bitmap) {
		cache.put(url, bitmap);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.news_listview_item, null);
			holder.imageView = (ImageView) convertView.findViewById(R.id.news_listView_img);
			holder.title = (TextView) convertView.findViewById(R.id.news_listView_title);
			holder.content = (TextView) convertView.findViewById(R.id.news_listView_content);
			holder.link = (TextView) convertView.findViewById(R.id.news_listView_link);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// 根据网络图片url 判断是从缓存取还是从网络取
		int imgNum = listData.get(position).getImageurls().size();
		if (imgNum > 0) {
			String url = listData.get(position).getImageurls().get(0).getUrl();
			holder.imageView.setTag(url);
			if (cache.get(url) == null) {
				LoadImage image = new LoadImage(holder.imageView, cache);
				image.execute(url);
			} else {
				holder.imageView.setImageBitmap(getBitMapFromCache(url));
			}
		} else {
			holder.imageView.setImageResource(R.drawable.icon);
		}
		
		holder.link.setText(listData.get(position).getLink());
		holder.title.setText(listData.get(position).getTitle());
		holder.content.setText(listData.get(position).getDesc());
		
		return convertView;
	}

	/** 存放控件 */
	public final class ViewHolder {
		public ImageView imageView;
		public TextView title, content,link;
	}

}
